﻿using System.Web.Mvc;
using Shell.DomainDot;
using Framework.Domain;
using Framework.Mvc.ViewModels;
using Framework.Poco;
using System.Collections.Generic;
using System.Linq;

namespace Framework.Mvc.Controllers
{
    public class AdminController : Controller
    {
        private IDomainDotFactory factory;

        public AdminController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 文章分类
        /// </summary>
        public ActionResult ArticleCategory(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var domain = factory.Create<SelectCategory>();
                var categories = domain.GetAllCategories(); ;
                var model = new ArticleCategoryViewModel();
                model.List = categories;
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        public ActionResult ArticleList(User currentUser, int currentPage, int pageSize)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<SelectArticle>();
                var articleAndCategoryList = new List<ArrangedArticleCategory>();
                foreach (var item in context.ArticleByCategory)
                {
                    var article = item.GetArticle();
                    var category = item.GetCategory();
                    var articleAndCategory = new ArrangedArticleCategory(article, category);
                    articleAndCategoryList.Add(articleAndCategory);
                }
                var list = articleAndCategoryList.OrderByDescending(item => item.Instance.Created);
                var model = new ArticleAndCategoryListViewModel(list, currentPage, pageSize, list.Count());
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }
    }
}
